add_rules("mode.debug", "mode.release")

local version=1.0
local package = "ly.audioplayer"

local opt_build_dep = getenv("OPT_BUILD_DEP") == "1"

local function nimble_add_deps(name)
    if (opt_build_dep)
    then
        add_deps(name)
    else
        add_packages(name)
     end
end

if (opt_build_dep)
then
    includes("../Collections-C")
    includes("../foundation")
    includes("../lv_ext")
    includes("../lvgl")
    includes("../interact_app")
    includes("../lv_bk")
else
    add_requires("pkgconfig::collections_c", {alias = "collections_c"})
    add_requires("pkgconfig::foundation", {alias = "foundation"})
    add_requires("pkgconfig::lv_ext7", {alias = "lv_ext7"})
    add_requires("pkgconfig::lvgl7", {alias = "lvgl7"})
    add_requires("pkgconfig::lv_bk", {alias = "lv_bk"})
    add_requires("pkgconfig::interact_app", {alias = "interact_app"})
end
add_requires("pkgconfig::alsa", {alias = "alsa"})
add_requires("pkgconfig::mad", {alias = "mad"})

target("audioplayer")
    set_kind("binary")
    add_includedirs(".", {public = true})
    add_headerfiles("*.hpp")
    add_files("*.cpp", "ring_buf.c", "demo/player.cpp")
    add_ldflags("-lpthread")
    nimble_add_deps("collections_c")
    nimble_add_deps("foundation")
    nimble_add_deps("lv_ext7")
    nimble_add_deps("lvgl7")
    nimble_add_deps("interact_app")
    nimble_add_deps("lv_bk")
    add_packages("alsa")
    add_packages("mad")

    on_install(function (target)
    end)

target("player_jni")
    add_includedirs("../jamvm_output/include", {public = true})
    set_kind("shared")
    add_includedirs(".")
    add_files("*.cpp", "ring_buf.c", "jni/*.cpp")
    add_linkdirs("kissfft")
    add_links("kissfft-float")
    add_ldflags("-lpthread")
    nimble_add_deps("collections_c")
    nimble_add_deps("foundation")
    nimble_add_deps("interact_app")
    add_packages("alsa")
    add_packages("mad")

    on_install(function (target)
        bundle_path = os.getenv("APPDIR")
        bundle_path = path.join(bundle_path, "ly.audioplayer")
        os.mkdir(bundle_path)

        os.cp("manifest.xml", bundle_path)

        os.mkdir(path.join(bundle_path, "lib"))
        os.cp(target:targetfile(), path.join(bundle_path, "lib"), {symlink = true})
        os.cp("kissfft/libkissfft-float.so", path.join(bundle_path, "lib"), {symlink = true, force=true})
        os.cp("kissfft/libkissfft-float.so.131", path.join(bundle_path, "lib"), {symlink = true, force=true})
        os.cp("kissfft/libkissfft-float.so.131.1.0", path.join(bundle_path, "lib"), {symlink = true})

        os.mkdir(path.join(bundle_path, "jar"))
        os.cp("build/app.jar", path.join(bundle_path, "jar"))

        os.mkdir(path.join(bundle_path, "assets"))
        os.cp("assets/icon", path.join(bundle_path, "assets"))
    end)